package com.bbva.gptf.back.dominio;

import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooDbManaged(automaticallyDelete = true)
@RooEntity(versionField = "", table = "TGPT012_GESTOR", schema = "DBSGPT", finders = { "findTgpt012GestorsByCdMancuerna" })
public class Tgpt012Gestor {

	public static Boolean eliminaGestor(final BigDecimal cdUsuario) {
		if (cdUsuario == null) {
			return false;
		}

		try {
			final StringBuffer qlString = new StringBuffer("DELETE FROM ");
			qlString.append("com.bbva.gptf.back.dominio.Tgpt012Gestor g ");
			qlString.append("where g.cdUsuario = :cdUsuario");

			entityManager().createQuery(qlString.toString())
					.setParameter("cdUsuario", cdUsuario).executeUpdate();
		} catch (final Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

	@Id
	@Column(name = "CD_USUARIO")
	private BigDecimal cdUsuario;

	@OneToOne
	@JoinColumn(name = "CD_USUARIO", nullable = false, insertable = false, updatable = false)
	@Fetch(FetchMode.JOIN)
	private Tgpt011Usuario tgpt011Usuario;

	@Column(name = "CD_MANCUERNA")
	private BigDecimal cdMancuerna;

	@ManyToOne
	@JoinColumn(name = "CD_MANCUERNA", referencedColumnName = "CD_USUARIO", insertable = false, updatable = false)
	private Tgpt012Gestor mancuerna;
}
